﻿using System;
using System.Data.SqlClient;
using System.Web.UI;

namespace 基本ADO.NET对象
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnConnect_Click(object sender, EventArgs e)
        {
            //Response.Write("Sql基本ADO.NET对象<br/>");
            const string connectionString = "server=.;database=schooldb;Trusted_Connection=SSPI";
            //Response.Write("建立SqlConnection对象<br/>");
            //SqlConnection sqlConnection = new SqlConnection();
            //Response.Write("根据连接字符串创建连接<br/>");
            //sqlConnection.ConnectionString = connectionString;
            //Response.Write("打开数据库连接<br/>");
            //sqlConnection.Open();
            //Response.Write("连接状态为：" + sqlConnection.State + "<br>");
            //Response.Write("连接字符串为：" + sqlConnection.ConnectionString + "<br>");
            //Response.Write("关闭数据库连接");
            //sqlConnection.Close();
            //Response.Write("关闭后的连接状态为：" + sqlConnection.State + "<br>");


            try
            {
                var connection = new SqlConnection(connectionString);
                connection.Open();
                connection.Close();
                connection.Close();
            }
            catch (Exception exception)
            {
                Response.Write(exception.Message);
                throw;
            }

            //using (SqlConnection sqlConnection2 = new SqlConnection(connectionString))
            //{
            //    sqlConnection.Open();
            //    sqlConnection.Close();
            //}//另一种写法
        }
    }
}